﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Constructor
{
    public partial class PropertiesPanel : UserControl
    {
        Frame _frame = null;
        public PropertiesPanel()
        {
            InitializeComponent();
            switch (Properties.Settings.Default.defaultFrameType)
            {
                case CommonLib.FrameType.Prototype:
                    cmb_type.SelectedIndex = 0;
                    break;
                case CommonLib.FrameType.Situation:
                    cmb_type.SelectedIndex = 1;
                    break;
                case CommonLib.FrameType.Exemplar:
                    cmb_type.SelectedIndex = 2;
                    break;
            }
        }
        public void Init(Frame frame)
        {
            Visible = true;
            _frame = frame;            
            lbl_frameName.Text = _frame.Name;
            txtName.ReadOnly = frame.Name == "Клетка" || frame.Name == "Объект";
            cmb_type.Enabled = !(frame.Name == "Клетка" || frame.Name == "Объект");
            txtName.Text = _frame.Name;
            label4.Visible = num_priority.Visible = frame.Type == CommonLib.FrameType.Situation;
            num_priority.Value = _frame.Priority;
            if (_frame.Name == "Поле")
            {
                tableLayoutPanel1.Visible = false;
                num_rang.Value = (int)Math.Sqrt(External.SearchFrame("Поле").Slots.Count());
            }
            else
                tableLayoutPanel1.Visible = true;
            switch (_frame.Type)
            {
                case CommonLib.FrameType.Prototype:
                    cmb_type.SelectedIndex = 0;
                    break;
                case CommonLib.FrameType.Situation:
                    cmb_type.SelectedIndex = 1;
                    break;
                case CommonLib.FrameType.Exemplar:
                    cmb_type.SelectedIndex = 2;
                    break;
            }
        }

        private void tableLayoutPanel1_Paint(object sender, PaintEventArgs e)
        {

        }
        public void Hide()
        {
            Visible = false;
        }
        private void cmb_type_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (_frame != null)
            {
                switch (cmb_type.SelectedIndex)
                {
                    case 0:
                        _frame.Type = CommonLib.FrameType.Prototype;
                        break;
                    case 1:
                        _frame.Type = CommonLib.FrameType.Situation;
                        break;
                    case 2:
                        _frame.Type = CommonLib.FrameType.Exemplar;
                        break;
                }
                _frame.RepaintNode(true);
                label4.Visible = num_priority.Visible = _frame.Type == CommonLib.FrameType.Situation;

            }
        }
        private void ChangeName(string name)
        {
            if (name == "")
            {
                MessageBox.Show("Имя не может быть пустым");
                if (_frame != null)
                    txtName.Text = _frame.Name;
            }
            else
                if (name.Contains("."))
                {
                    MessageBox.Show("Запрещенный символ \".\"");
                    if (_frame != null)
                        txtName.Text = _frame.Name;
                }
            if (_frame != null)
            {
                _frame.Name = name;
                lbl_frameName.Text = name;
                _frame.RepaintNode(true);
            }
        }
        private void txtName_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                ChangeName(txtName.Text.Trim());
        }

        private void txtName_Leave(object sender, EventArgs e)
        {
            ChangeName(txtName.Text.Trim());
        }

        private void btn_slots_Click(object sender, EventArgs e)
        {
            if (_frame != null)
            {
                new EditorSlot(_frame) { TopMost = FindForm().TopMost }.ShowDialog();
            }
        }

        private void num_rang_ValueChanged(object sender, EventArgs e)
        {
            External.constructor.SetRankField((int)num_rang.Value);
        }

        private void num_priority_ValueChanged(object sender, EventArgs e)
        {
            _frame.Priority =(int) num_priority.Value;
        }
    }
}
